package com.xu.crawler.dao.impl;

import com.xu.crawler.dao.HkStockDao;
import com.xu.crawler.dao.NewsDao;
import com.xu.crawler.dao.entity.HkStockDO;
import com.xu.crawler.dao.entity.NewsDO;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by clear on 2018/7/11.
 */
@Repository
public class HkStockDaoImpl implements HkStockDao {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public void batchSaveStocks(List<HkStockDO> stockDOS) {
        mongoTemplate.insertAll(stockDOS);
    }

    @Override
    public long count() {
        return mongoTemplate.count(new Query(), HkStockDO.class);
    }

    @Override
    public List<HkStockDO> queryAll() {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.ASC, "code"));
        return mongoTemplate.find(query, HkStockDO.class);
    }

    @Override
    public HkStockDO queryByCode(String code) {
        Query query = new Query();
        query.addCriteria(Criteria.where("code").is(code));
        return mongoTemplate.findOne(query, HkStockDO.class);
    }
}
